<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="formRef" label-width="130px" :model="form" :rules="rules">
      <!-- <el-form-item label="空间类型" prop="HostType">
        <el-input v-model="form.HostType" :disabled="disabled" />
      </el-form-item> -->
      <el-form-item label="厂商" prop="Supplier">
        <el-select v-model="form.Supplier" :disabled="disabled">
          <el-option label="西部数码" value="西部数码" />
          <el-option label="中国频道" value="中国频道" />
          <el-option label="阿里云" value="阿里云" />
          <el-option label="商务中国" value="商务中国" />
          <el-option label="新网" value="新网" />
        </el-select>
      </el-form-item>
      <el-form-item label="空间名称" prop="HostName">
        <el-input v-model="form.HostName" :disabled="disabled" />
      </el-form-item>
      <el-form-item label="绑定域名" prop="BindDomains">
        <el-input v-model="form.BindDomains" :disabled="disabled" />
      </el-form-item>
      <el-form-item label="空间大小" prop="HostSize">
        <el-input v-model="form.HostSize" :disabled="disabled" />
      </el-form-item>
      <el-form-item label="管理地址" prop="ManageUrl">
        <el-input v-model="form.ManageUrl" :disabled="disabled" />
      </el-form-item>
      <el-form-item label="管理账户" prop="ManageAccount">
        <el-input v-model="form.ManageAccount" :disabled="disabled" />
      </el-form-item>
      <el-form-item label="管理密码" prop="ManagePassword">
        <el-input v-model="form.ManagePassword" :disabled="disabled" />
      </el-form-item>
      <el-form-item label="FTP地址" prop="FtpIPAddr">
        <el-input v-model="form.FtpIPAddr" :disabled="disabled" />
      </el-form-item>
      <el-form-item label="FTP端口" prop="FtpPort">
        <el-input v-model="form.FtpPort" :disabled="disabled" />
      </el-form-item>
      <el-form-item label="FTP账号" prop="FtpAccount">
        <el-input v-model="form.FtpAccount" :disabled="disabled" />
      </el-form-item>
      <el-form-item label="FTP密码" prop="FtpPassword">
        <el-input v-model="form.FtpPassword" :disabled="disabled" />
      </el-form-item>
      <!-- <el-form-item v-if="!disabled" label="实际服务期限">
        <el-date-picker
          v-model="form.ServiceDates"
          type="daterange"
          start-placeholder="Start date"
          end-placeholder="End date"
          :default-time="defaultTime"
          :disabled="disabled"
        />
      </el-form-item> -->
      <el-form-item v-if="form.isShowPassword" label="查看密码">
        <el-input v-model="form.ShowPassword" disabled />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { saveHost, showHost } from '@/api/website'

  export default defineComponent({
    name: 'Host',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')
      const state = reactive({
        disabled: false,
        formRef: null,
        form: {},
        storeForm: {},
        rules: {
          HostType: [{ required: true, trigger: 'blur', message: '空间类型' }],
          Supplier: [{ required: true, trigger: 'blur', message: '厂商' }],
          HostName: [{ required: true, trigger: 'blur', message: '空间名称' }],
          BindDomains: [
            { required: true, trigger: 'blur', message: '绑定域名' },
          ],
          HostSize: [{ required: true, trigger: 'blur', message: '空间大小' }],
          ManageUrl: [{ required: true, trigger: 'blur', message: '管理地址' }],
          ManageAccount: [
            { required: true, trigger: 'blur', message: '管理账号' },
          ],
          ManagePassword: [
            { required: true, trigger: 'blur', message: '管理密码' },
          ],
          FtpIPAddr: [{ required: true, trigger: 'blur', message: 'FTP地址' }],
          FtpPort: [{ required: true, trigger: 'blur', message: 'FTP端口' }],
          FtpAccount: [{ required: true, trigger: 'blur', message: 'FTP账号' }],
          FtpPassword: [
            { required: true, trigger: 'blur', message: 'FTP密码' },
          ],
          ServiceDates: [
            { required: true, trigger: 'blur', message: '请选择服务期限' },
          ],
        },
        title: '',
        dialogFormVisible: false,
        StoreOptions: [],
        BindName: '',
      })

      const showEdit = async (row, flag, disabled) => {
        state.disabled = disabled
        console.log(row, '主机')

        state.title = row.ProductName
        state.storeForm = Object.assign({}, row)
        state.form.CustomerID = state.storeForm.CustomerID
        state.form.OrderID = state.storeForm.OrderID
        state.form.OrderListID = state.storeForm.OrderListID
        state.form.BindStoreID = state.storeForm.BindStoreID
        state.form.HostID = row.BindID
        console.log(state.form, '123')
        if (flag) {
          await gitShowHost()
        }
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {}
        state.dialogFormVisible = false
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { msg } = await saveHost(state.form)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')

            close()
          }
        })
      }

      const getData = async () => {
        state.listLoading = true

        const $params = {
          CustomerID: state.form.CustomerID,
          ProductID: state.form.ProductID,
        }
        const {
          data: { StoreOptions },
        } = await StoreSelect($params)
        state.StoreOptions = StoreOptions
        state.listLoading = false
      }
      const gitShowHost = async () => {
        const dataMsg = {
          HostID: state.form.HostID,
        }
        const { data } = await showHost(dataMsg)
        state.form = data
        state.form.ServiceDates = [
          state.storeForm.ServiceStartDate,
          state.storeForm.ServiceEndDate,
        ]
        state.form.CustomerID = state.storeForm.CustomerID
        state.form.OrderID = state.storeForm.OrderID
        state.form.OrderListID = state.storeForm.OrderListID
        state.form.BindStoreID = state.storeForm.BindStoreID
        state.form.HostID = state.storeForm.BindID
        console.log(state.form, '1111')
      }

      return {
        ...toRefs(state),
        showEdit,
        gitShowHost,
        getData,
        close,
        save,
      }
    },
  })
</script>
